
package com.mal.huntthewumpus;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Illustrates how a command line interface might work.
 * @author JPTM
 */
public class CmdLineDialog {
    
    public static void main(String[] args) {
        initMaps();
        System.out.println("Hi !");
        Scanner scln = new Scanner(System.in);
        
        do {
           System.out.print(prompt);
           String s = scln.nextLine().trim();
           Scanner scstr = new Scanner(s);
           if (!scstr.hasNext()) {
               // blank line
               continue;
           }
           
           String tk = scstr.next().toLowerCase();
           Character cVal = cmdMap.get(tk);
           if (cVal == null) {
               System.out.printf("I can't recognise command \"%s\".%n", tk);
               help();
           } else {
               switch (cVal) {
                   case 'A':
                     ascend();
                     break;
                   case 'G':
                     if (!scstr.hasNext()) { // check passage missing
                        System.out.println("\"Go\" needs a passage.");
                        help();
                     } else {
                         String ptk = scstr.next().toLowerCase();
                         Passage p = passMap.get(ptk);
                         if (p == null) {  // check invalid passage
                             System.out.printf("I don't recognise "
                                     + "passage \"%s\".%n", ptk);
                             help();
                         } else {
                             go(p);
                         }
                     }
                     break;
                   case 'H':
                     help();
                     break;
                   case 'L':
                     loot();
                     break;
                   case 'Q':
                     quit();
                     break;
                   case 'S':
                     if (!scstr.hasNext()) { // check weapon missing
                        System.out.println("\"Shoot\" needs a "
                                + "weapon and a passage.");
                        help();
                     } else {
                         String wtk = scstr.next().toLowerCase();
                         Weapon w = weapMap.get(wtk);
                         if (w == null) {  // check invalid weapon
                             System.out.printf("I don't recognise "
                                     + "weapon \"%s\".%n", wtk);
                             help();
                         } else if (!scstr.hasNext()) { // check passage missing
                             System.out.println("\"Shoot\" needs "
                                              + "a passage.");
                             help();
                         } else {
                             String ptk = scstr.next().toLowerCase();
                             Passage p = passMap.get(ptk);
                             if (p == null) {  // check invalid passage
                                 System.out.printf("I don't recognise "
                                                 + "passage \"%s\".%n", ptk);
                                 help();
                             } else {
                                 shoot(w,p);
                             }
                         }
                     }
                     break;
                   default:
                     help();
                     break;
               }
           }     
        } while(true);
        
    }
    
    private static void ascend() {
        System.out.println("Ascent completed !");
    }
    
    private static void go(Passage p) {
        System.out.printf("Traversed passage %s.%n", p);
    }
    
    private static void help() {
        System.out.println("Usage info for ascend, go, "
                         + "help, loot, quit and shoot.");
    }
    
    private static void loot() {
        System.out.println("You have now looted this room.");
    }
    
    private static void quit() {
        System.out.println("Bye !");
        System.exit(0);
    }
    
    private static void shoot(Weapon w, Passage p) {
        System.out.printf("Shot %s into %s passage.%n", w, p);
    }
    
    private static void initMaps () {
        cmdMap.put("a",'A');
        cmdMap.put("ascend",'A');
        cmdMap.put("g",'G');
        cmdMap.put("go",'G');
        cmdMap.put("h",'H');
        cmdMap.put("help",'H');
        cmdMap.put("l",'L');
        cmdMap.put("loot",'L');
        cmdMap.put("q",'Q');
        cmdMap.put("quit",'Q');
        cmdMap.put("s",'S');
        cmdMap.put("shoot",'S');
        
        weapMap.put("a",Weapon.ARROW);
        weapMap.put("arrow",Weapon.ARROW);
        weapMap.put("d",Weapon.DOG);
        weapMap.put("dog",Weapon.DOG);
        
        passMap.put("n",Passage.N);
        passMap.put("s",Passage.S);
        passMap.put("e",Passage.E);
        passMap.put("w",Passage.W);
    }
    
    private static Map<String,Character> cmdMap = new HashMap<>();
    private static Map<String,Weapon> weapMap = new HashMap<>();
    private static Map<String,Passage> passMap = new HashMap<>();
    private static String prompt = "--> ";
    private enum Passage{N, S, E, W};
    private enum Weapon {ARROW, DOG};
    
    }
    

